<?php

namespace App\Admin\Extensions;

use Encore\Admin\Admin;

class PreCuishou
{
    public function __construct($name, $oid, $type)
    {
        $this->name = $name;
        $this->oid = $oid;
        $this->type = $type;
    }
    protected function script()
    {
        return <<<SCRIPT

layui.use('layer', function(){
        var layer = layui.layer;
        
        $('.precuishou').click(function () {
           var that = this;
            var orderid = $(that).attr('data-id');
            var user_name = $(that).attr('data-name');
           
            var index = layer.open({
                content: '/admin/collectorsys/precuishou',
                type: 2,
                title: '<\span style="color:red">'+user_name+'</\span>',
                area: ['300px', '220px'],
                btn: ['确定', '取消'],
                yes: function(index, layero){
                    var collector_type = $(layero).find("iframe").contents().find("#type").val();
                    var collector_id = $(layero).find("iframe").contents().find("#collectors").val();
                    if(collector_id < 1){
                        layer.msg('没有选择催收员');
                        return;
                    }
                    $.ajax({
                        url: '/admin/collectorsys/precuishou',
                        type: 'post',
                        data: {
                            type: collector_type,
                            orderid: orderid,
                            collector_id: collector_id
                        },
                        success: function (res) {
                            if(res.code) {
                                layer.msg(res.text)
                                layer.close(index);
                                return;
                            }
                            $(that).css("cssText", "color:#000 !important;");
                            $(that).css("cssText", "background-color:#FF8400 !important;");
                            $(that).text('已分配');
                            layer.msg(res.text);
                            layer.close(index);
                        }
                    })
                }
            });         
       });
});

SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());
        if($this->type == 1){
            return "&nbsp;<a href='javascript:void(0);'><button data-name='{$this->name}' data-id='{$this->oid}'  class='precuishou layui-btn layui-btn-xs' style='color: #fff !important;background-color: #5FB878 !important;'>分配</button></a>";
        }elseif($this->type == 2){
            return "&nbsp;<a href='javascript:void(0);'><button data-name='{$this->name}' data-id='{$this->oid}'  class='precuishou layui-btn layui-btn-xs' style='color: #fff !important;background-color: #FF8400 !important;'>已分配</button></a>";
        }
    }

    public function __toString()
    {
        return $this->render();
    }
}